package com.yan.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yan.controller.viewModel.login.UserModel;
import com.yan.controller.viewModel.user.BorrowInfo;
import com.yan.controller.viewModel.user.ReserveInfo;
import com.yan.controller.viewModel.user.SuggestBuyInfo;
import com.yan.util.CastMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yan.util.PageConstant;
import com.yan.dao.mapper.AdminMapper;
import com.yan.dao.entity.Admin;
import com.yan.dao.entity.Borrows;
import com.yan.dao.entity.PersonMessage;
import com.yan.dao.entity.Reserve;
import com.yan.dao.entity.SuggestBuy;
import com.yan.dao.entity.Users;
import com.yan.service.AdminService;
import com.yan.vo.Page;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminMapper adminMapper;
	

	/*
	 * 查询借书数据
	 */
	@Override
	public Page getBorrows(BorrowInfo borrows) {
		// 开始
		Borrows borrow = (Borrows) CastMapper.CastTo(Borrows.class,borrows);
		Page page = new Page();
		if (borrows.getCurrentPage() == 0) {
			borrows.setCurrentPage(PageConstant.PAGE_NUM);
		}
		int start = (borrows.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		borrows.setStart(start);
		
		List<Borrows> list = adminMapper.getBorrows(borrow);
		page.setList(list);
		int count = adminMapper.getCount(borrow);
		//计算总页数
		int counPage = count / PageConstant.PAGE_SIZE;
		//确保不会有数据遗漏
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		page.setCount(count);
		return page;
	}

	/*
	 * 允许借书操作
	 */
	@Override
	public void updateBorrows(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.updateBorrows(id);
	}

	/*
	 * 不允许借书
	 */
	@Override
	public void notAllowBorrows(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.notAllowBorrows(id);
	}

	/*
	 * 将允许借的书的信息插入还书表
	 */
	@Override
	public void inserBackBooks(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.inserBackBooks(id);
	}

	/*
	 * 查询还书表
	 */
	@Override
	public Page getBackBooks(BorrowInfo borrows) {
		Borrows borrow= (Borrows)CastMapper.CastTo(Borrows.class,borrows);
		Page page = new Page();
		if (borrows.getCurrentPage() == 0) {
			borrows.setCurrentPage(PageConstant.PAGE_NUM);
		}
		int start = (borrows.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		borrows.setStart(start);
		List<Borrows> list = adminMapper.getBackBooks(borrow);
		page.setList(list);
		int count = adminMapper.getCount2(borrow);
		int counPage = count / PageConstant.PAGE_SIZE;
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		page.setCount(count);
		return page;
	}
	
	
    /*
     * 确认还书
     */
	@Override
	public void allowBackBook(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.allowBackBook(id);
	}
	
	
	
   /*
    * 查询预定书籍数据
   */
	@Override
	public List<ReserveInfo> getAllReserve() {
		// TODO Auto-generated method stub
		List<Reserve> reserveList = adminMapper.getAllReserve();
		List<ReserveInfo> reserveInfoList = new ArrayList<>(reserveList.size());
		for(Reserve reserve:reserveList){
			reserveInfoList.add((ReserveInfo) CastMapper.CastTo(ReserveInfo.class,reserve));
		}
		return reserveInfoList;
	}

	/*
	 * 同意预定
	 */
	@Override
	public void updateReserveBook(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.updateReserveBook(id);
	}

	/*
	 * 查询管理员是否存在
	 */
	@Override
	public UserModel getAdmin(UserModel user) {
		// TODO Auto-generated method stub
		Admin admin = adminMapper.getAdmin((Users) CastMapper.CastTo(Admin.class,user));
		return (UserModel) CastMapper.CastTo(UserModel.class,admin);
	}

	/*
	 * 不同意预定
	 */
	@Override
	public void notAllowReserveBook(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.notAllowReserveBook(id);
	}

	/*
	 * 查询借书id
	 */
	@Override
	public int selectBorrowsId(Integer id) {
		// TODO Auto-generated method stub
		return adminMapper.selectBorrowsId(id);
	}

	/*
	 * 查询书籍id
	 */
	@Override
	public int selectBooksId(Integer id) {
		// TODO Auto-generated method stub
		return adminMapper.selectBooksId(id);
	}

	/*
	 * 加一操作
	 */
	@Override
	public void updateBooks(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.updateBooks(id);
	}

	/*
	 * 减一操作
	 */
	@Override
	public void descBooks(Integer id) {
		// TODO Auto-generated method stub
		adminMapper.descBooks(id);
	}

	/*
	 * 更新借书借书时间
	 */
	@Override
	public void updateBorrowsTime(BorrowInfo borrow) {
		// TODO Auto-generated method stub
		adminMapper.updateBorrowsTime((Borrows)CastMapper.CastTo(Borrows.class,borrow));
	}

	/*
	 * 更新预定借书时间
	 */
	@Override
	public void updateReserveTime(ReserveInfo reserve) {
		// TODO Auto-generated method stub
		adminMapper.updateReserveTime((Reserve) CastMapper.CastTo(Reserve.class,reserve));
	}

	
   //查询建构表信息
	@Override
	public Page getSuggestBuys(SuggestBuyInfo suggestBuy) {
		// TODO Auto-generated method stub
		Page page=new Page();
		SuggestBuy suggest =(SuggestBuy) CastMapper.CastTo(SuggestBuy.class,suggestBuy);
		
		if(suggestBuy.getCurrentPage()==0) {
			suggestBuy.setCurrentPage(PageConstant.PAGE_NUM);
		}
		
		int start = (suggestBuy.getCurrentPage()-1)* PageConstant.PAGE_SIZE;
		suggestBuy.setStart(start);
		
		List<SuggestBuy> list=adminMapper.selectSuggest(suggest);
		page.setList(list);
		int count=adminMapper.getSuggestBuyCount();
		int counPage=count/PageConstant.PAGE_SIZE;
		if(count%PageConstant.PAGE_SIZE!=0) {
			counPage++;
		}
		
		page.setTotalPage(counPage);
		page.setCount(count);
		
		return page;
		
		
	}

	//允许购买
	@Override
	public void allowBuy(int id) {
		// TODO Auto-generated method stub
		adminMapper.allowBuy(id);
		
	}

	//不允许购买
	@Override
	public void notAllowBuy(int id) {
		// TODO Auto-generated method stub
		adminMapper.notAllowBuy(id);
	}

	//查询用户信息
	@Override
	public Page getAllPersonMessage(BorrowInfo borrows) {
		// TODO Auto-generated method stub
		Page page=new Page();
		Borrows borrow = (Borrows) CastMapper.CastTo(Borrows.class,borrows);
		if(borrows.getCurrentPage()==0) {
			borrows.setCurrentPage(PageConstant.PAGE_NUM);
		}
		
		int start = (borrows.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		borrows.setStart(start);
		
		List<PersonMessage> list = adminMapper.getAllPersonMessage(borrow);
		page.setList(list);
		
		int count = adminMapper.getCount3(borrow);
		int counPage = count / PageConstant.PAGE_SIZE;//计算总页数
		//确保不会有数据遗漏
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		page.setCount(count);
		return page;
	}

	/*
	 * 查询指定用户的借书信息
	 */
	@Override
	public Page getUserBorrows(BorrowInfo borrows) {
		Page page = new Page();
		Borrows borrow = (Borrows) CastMapper.CastTo(Borrows.class,borrows);
		if (borrows.getCurrentPage() == 0) {
			borrows.setCurrentPage(PageConstant.PAGE_NUM);
		}
		int start = (borrows.getCurrentPage() - 1) * PageConstant.PAGE_SIZE;
		borrows.setStart(start);
		//System.out.println("start:"+start);
		
		List<Borrows> list = adminMapper.getUserBorrowsBooks(borrow);
		page.setList(list);
		//System.out.println("在AdminServiceImpl中的为"+page.getList().size());
		
		int count = adminMapper.getCount2(borrow);
		int counPage = count / PageConstant.PAGE_SIZE;
		if (count % PageConstant.PAGE_SIZE != 0) {
			counPage++;
		}
		// 总页数
		page.setTotalPage(counPage);
		// 总记录数
		page.setCount(count);
		return page;
	}

	

}
